//
//  SAUserExpireView.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/7/1.
//

#import "SAUserExpireView.h"
#import "SAUserExpireModel.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SALoginVC.h"

@interface SAUserExpireView ()

@property (nonatomic, strong) SAUserExpireModel *model;

@end

@implementation SAUserExpireView

-(instancetype)initWithUserExpireModel:(SAUserExpireModel *)model {
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]) {
        self.model = model;
        [self initUI];
    }
    return self;
}

#pragma mark - event
-(void)clickedCopyBtn {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.model.wx;
    [Prompt popPromptViewWithMsg:@"已复制到剪切板!" duration:3];
}

-(void)clickedIphoneBtn {
    NSMutableString *str= [[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.model.hotline];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:[NSDictionary dictionary] completionHandler:nil];
}

- (void)showView:(UIViewController *)Sview {
    if(Sview == nil){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)shareDissTapEvent {
    if ([self.model.termType isEqualToString:@"2"]) {
        [UIView animateWithDuration:0.5f animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            !self.removeBlock ?: self.removeBlock();
        }];
    }
}

-(void)shareDissTapEvent2 {
    if ([self.model.termType isEqualToString:@"3"]) {
        [SAValueManger shareInStance].userInfo = nil;
        [kUserDefault setObject:@"" forKey:kAotuLoginKey];
        UIWindow *window =  [UIApplication sharedApplication].keyWindow;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[SALoginVC new]];
        window.rootViewController = nav;
        [window makeKeyAndVisible];
    }else {
        [UIView animateWithDuration:0.5f animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            !self.removeBlock ?: self.removeBlock();
        }];
    }
}

#pragma mark - initUI
-(void)initUI {
    self.userInteractionEnabled = YES;
    self.backgroundColor = kColor(0, 0, 0, 0.6);
   
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth - kScaleNum(265))/2, (kScreenHeight - kScaleNum(150))/2, kScaleNum(265), kScaleNum(150))];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.masksToBounds = YES;
    whiteView.layer.cornerRadius = kScaleNum(5);
    [self addSubview:whiteView];
    
    UIButton *deleteXBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteXBtn.frame = CGRectMake(kScaleNum(225), 0, kScaleNum(40), kScaleNum(40));
    [deleteXBtn setImage:[UIImage imageNamed:@"dueto_x"] forState:UIControlStateNormal];
    [deleteXBtn addTarget:self action:@selector(shareDissTapEvent2) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:deleteXBtn];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(30, 30, 205, 40)];
    titleLab.textColor = kBlackColor;
    titleLab.font = kFont(14);
    titleLab.numberOfLines = 0;
    titleLab.text = [NSString stringWithFormat:@"您的账户于%@到期,为了不影响使用,请及时联系客服续费",self.model.dueDate];
    [whiteView addSubview:titleLab];
    [titleLab setAttributedStringWithSubString:self.model.dueDate color:kPinkColor];
    
    UILabel *wxLab = [[UILabel alloc] initWithFrame:SARectMake(30, 84, 170, 15)];
    wxLab.textColor = kLightgGrayColor;
    wxLab.font = kFont(14);
    wxLab.text = [NSString stringWithFormat:@"客服微信：%@",self.model.wx];
    [whiteView addSubview:wxLab];
    
    UILabel *csLab = [[UILabel alloc] initWithFrame:SARectMake(30, 108, 170, 15)];
    csLab.textColor = kLightgGrayColor;
    csLab.font = kFont(14);
    csLab.text = [NSString stringWithFormat:@"客服手机：%@",self.model.hotline];
    [whiteView addSubview:csLab];
    
    UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    copyBtn.frame = SARectMake(182, 72, 65, 39);
    [copyBtn setTitle:@"复制" forState:UIControlStateNormal];
    [copyBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    copyBtn.titleLabel.font = kFont(14);
    [copyBtn addTarget:self action:@selector(clickedCopyBtn) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:copyBtn];
    
    UIButton *iphoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    iphoneBtn.frame = SARectMake(182, 96, 65, 39);
    [iphoneBtn setImage:[UIImage imageNamed:@"bule_iphone"] forState:UIControlStateNormal];
    [iphoneBtn addTarget:self action:@selector(clickedIphoneBtn) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:iphoneBtn];
}

@end
